﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using Syufy.BINLookup.Web.Interfaces;

namespace Syufy.BINLookup.Web.Tests.Mocks
{
    public class MockRepository<TEntity> : IGenericRepository<TEntity> where TEntity : class
    {
        internal List<TEntity> _context;

        public MockRepository(List<TEntity> context)
        {
            _context = context;
        }

        public void Delete(TEntity entityToDelete)
        {
            _context.Remove(entityToDelete);
        }

        public void Delete(object id)
        {
            _context.RemoveAt(0);
        }

        public List<TEntity> Get(Expression<Func<TEntity, bool>> filter = null, Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null, params Expression<Func<TEntity, object>>[] includes)
        {
            return _context.AsQueryable().Where(filter).ToList();
        }

        public TEntity GetByID(object id)
        {
            return _context.FirstOrDefault();
        }

        public TEntity GetFirstOrDefault(Expression<Func<TEntity, bool>> filter = null, params Expression<Func<TEntity, object>>[] includes)
        {
            return _context.AsQueryable().FirstOrDefault(filter);
        }

        public void Insert(TEntity entity)
        {
            _context.Add(entity);
        }

        public IQueryable<TEntity> Query(Expression<Func<TEntity, bool>> filter = null, Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null, params Expression<Func<TEntity, object>>[] includes)
        {
            return _context.AsQueryable().Where(filter);
        }

        public void Update(TEntity entityToUpdate)
        {
            // Do nothing;
        }
    }
}
